//
//  NotifyToast.m
//  location
//
//  Created by Dongxiang Xu on 2025/9/29.
//

#import "NotifyToast.h"
#import "XDXLocationManager.h"

static NotifyToast *shareToast = nil;
@interface NotifyToast ()

@property (nonatomic,strong) UIImageView *img;
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIButton *button;

@end

@implementation NotifyToast

+ (instancetype)shareNotifyToast
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (shareToast == nil) {
            shareToast = [[self alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-263)/2, -48, 263, 48)];
            shareToast.layer.masksToBounds = YES;
            [shareToast.layer setCornerRadius:shareToast.height/2];
            shareToast.backgroundColor = [GraryHeavyLightColor colorWithAlphaComponent:0.6];
            [WINDOW addSubview:shareToast];
            
            shareToast.img = [[UIImageView alloc] initWithFrame:CGRectMake(18, (shareToast.height-18)/2, 18, 18)];
            [shareToast addSubview:shareToast.img];
            
            shareToast.label = [[UILabel alloc] initWithFrame:CGRectMake(44, 0, 120, shareToast.height)];
            shareToast.label.font = REAL_MEDIUM_FONT(13);
            shareToast.label.textColor = RealWhiteColor;
            [shareToast addSubview:shareToast.label];
            
            shareToast.button = [[UIButton alloc] initWithFrame:CGRectMake(shareToast.label.maxX+16, 0, 28, shareToast.height)];
            [shareToast.button setTitle:@"前往" forState:UIControlStateNormal];
            [shareToast.button.titleLabel setFont:REAL_MEDIUM_FONT(13)];
            [shareToast.button setTitleColor:RedColor forState:UIControlStateNormal];
            [shareToast addSubview:shareToast.button];
            [[shareToast.button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                [[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@NO} completionHandler:nil];
            }];
        }
    });
    if ([[XDXLocationManager shareInstance].locationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedAlways) {
        shareToast.hidden = NO;
        if ([[XDXLocationManager shareInstance].locationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse) {
            shareToast.img.image = [UIImage imageNamed:@"warnning"];
            shareToast.label.text = @"需要将位置权限设为“始终允许”";
        }else{
            shareToast.img.image = [UIImage imageNamed:@"notify_bling"];
            shareToast.label.text = @"打开“通知权限”，接收动态";
        }
    }else if ( [[XDXLocationManager shareInstance].locationManager accuracyAuthorization]==CLAccuracyAuthorizationReducedAccuracy) {
        shareToast.hidden = NO;
        shareToast.img.image = [UIImage imageNamed:@"warnning"];
        shareToast.label.text = @"需要打开“精确定位”";
    }else{
        shareToast.hidden = YES;
    }
    
    [shareToast.label sizeToFit];
    [shareToast.label setFrame:CGRectMake(44, 0, shareToast.label.width, shareToast.height)];
    [shareToast.button setFrame:CGRectMake(shareToast.label.maxX+16, 0, 28, shareToast.height)];
    [shareToast setFrame:CGRectMake((SCREEN_WIDTH-(shareToast.button.maxX+18))/2, shareToast.y, shareToast.button.maxX+18, 48)];
   
    return shareToast;
}

- (void)show
{
    if (shareToast.hidden == NO) {
        [UIView animateWithDuration:0.3 animations:^{
            shareToast.y = STATUSHEIGHT;
        }];
    }
   
}

- (void)dismiss
{
    if (shareToast.hidden == NO) {
        [UIView animateWithDuration:0.3 animations:^{
            shareToast.y = -48;
        }];
    }
}

@end
